package com.vesystem.vegua.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vesystem.vegua.exceptionConfig.ErrorCode;
import com.vesystem.vegua.exceptionConfig.ParameterInvalid;
import com.vesystem.vegua.modules.common.entity.ServersMonitoring;
import com.vesystem.vegua.modules.common.mapper.ServersMonitoringMapper;
import com.vesystem.vegua.modules.common.service.IServersMonitoringService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hcy
 * @since 2020-12-29
 */
@Service
public class ServersMonitoringServiceImpl extends ServiceImpl<ServersMonitoringMapper, ServersMonitoring> implements IServersMonitoringService {

    @Autowired
    private ServersMonitoringMapper serversMonitoringMapper;

    public void addServersMonitoring(ServersMonitoring serversMonitoring){
        QueryWrapper<ServersMonitoring> qw = new QueryWrapper<>();
        qw.eq("serv_ip",serversMonitoring.getServIp()).eq("serv_port",serversMonitoring.getServPort());
        ServersMonitoring db_ser = serversMonitoringMapper.selectOne(qw);
        if ( db_ser !=null ){
            throw new ParameterInvalid(ErrorCode.DATA_ALERADY_EXIST);
        }
        serversMonitoringMapper.insert(serversMonitoring);
    }

    public void updateServersMonitoring(ServersMonitoring serversMonitoring){
        ServersMonitoring db_ser = serversMonitoringMapper.selectById(serversMonitoring.getId());
        if ( db_ser == null ){
            throw new ParameterInvalid(ErrorCode.DATA_NOT_EXIST);
        }
        serversMonitoringMapper.updateById( serversMonitoring );
    }

    public void deleteServersMonitoringById(Integer id){
        ServersMonitoring db_ser = serversMonitoringMapper.selectById(id);
        if ( db_ser == null ){
            throw new ParameterInvalid(ErrorCode.DATA_NOT_EXIST);
        }
        serversMonitoringMapper.deleteById(id);
    }

    public Page<ServersMonitoring> getAllServersInfo(Page<ServersMonitoring> page,String serverIp,String serverName){
        QueryWrapper<ServersMonitoring> qw = new QueryWrapper<>();
        qw.like("serv_ip",serverIp).like("serv_name",serverName);
        return serversMonitoringMapper.selectPage(page,qw);
    }

    public ServersMonitoring getServersInfoById(Integer id){
        return serversMonitoringMapper.selectById(id);
    }




}
